{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-12-24T18:44:08.286174Z",
     "start_time": "2023-12-24T18:44:07.655263Z"
    }
   },
   "outputs": [],
   "source": [
    "import json  \n",
    "import numpy as np\n",
    "from tqdm import tqdm\n",
    "import pandas as pd\n",
    "import csv"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# init"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-12-24T18:44:09.924562Z",
     "start_time": "2023-12-24T18:44:09.915870Z"
    }
   },
   "outputs": [],
   "source": [
    "q_lst = []\n",
    "a_lst = []\n",
    "\n",
    "df = pd.DataFrame(columns=['prompt', 'answer'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# shibing624 Dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-12-24T18:46:49.716825Z",
     "start_time": "2023-12-24T18:46:36.546574Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "1949972it [00:13, 148255.79it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "this sft dataset size is: 1949972\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "data_bf_length = len(q_lst)\n",
    "sft_data_path = './sft_data/medical/shibing624/finetune/train_zh_0.json'\n",
    "with open(sft_data_path,'r',encoding='utf-8') as f:\n",
    "    for row in tqdm(f):\n",
    "        line = json.loads(row)\n",
    "        q = line['instruction']+line['input']\n",
    "        a = line['output']\n",
    "        if isinstance(q, str) and isinstance(a, str):\n",
    "            q_lst.append(q)\n",
    "            a_lst.append(a)\n",
    "data_af_length = len(q_lst)\n",
    "\n",
    "print('this sft dataset size is:', data_af_length - data_bf_length)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-12-24T18:46:51.967349Z",
     "start_time": "2023-12-24T18:46:51.003864Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "116617it [00:00, 122559.54it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "this sft dataset size is: 116617\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "data_bf_length = len(q_lst)\n",
    "sft_data_path = './sft_data/medical/shibing624/finetune/train_en_1.json'\n",
    "with open(sft_data_path,'r',encoding='utf-8') as f:\n",
    "    for row in tqdm(f):\n",
    "        line = json.loads(row)\n",
    "        q = line['instruction']+line['input']\n",
    "        a = line['output']\n",
    "        if isinstance(q, str) and isinstance(a, str):\n",
    "            q_lst.append(q)\n",
    "            a_lst.append(a)\n",
    "data_af_length = len(q_lst)\n",
    "print('this sft dataset size is:', data_af_length - data_bf_length)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-12-24T18:46:55.429747Z",
     "start_time": "2023-12-24T18:46:55.415012Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "500it [00:00, 105959.58it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "this sft dataset size is: 500\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "data_bf_length = len(q_lst)\n",
    "sft_data_path = './sft_data/medical/shibing624/finetune/valid_zh_0.json'\n",
    "with open(sft_data_path,'r',encoding='utf-8') as f:\n",
    "    for row in tqdm(f):\n",
    "        line = json.loads(row)\n",
    "        q = line['instruction']+line['input']\n",
    "        a = line['output']\n",
    "        if isinstance(q, str) and isinstance(a, str):\n",
    "            q_lst.append(q)\n",
    "            a_lst.append(a)\n",
    "data_af_length = len(q_lst)\n",
    "print('this sft dataset size is:', data_af_length - data_bf_length)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-12-24T18:46:58.686258Z",
     "start_time": "2023-12-24T18:46:58.668958Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "500it [00:00, 74198.70it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "this sft dataset size is: 500\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "data_bf_length = len(q_lst)\n",
    "sft_data_path = './sft_data/medical/shibing624/finetune/valid_en_1.json'\n",
    "with open(sft_data_path,'r',encoding='utf-8') as f:\n",
    "    for row in tqdm(f):\n",
    "        line = json.loads(row)\n",
    "        q = line['instruction']+line['input']\n",
    "        a = line['output']\n",
    "        if isinstance(q, str) and isinstance(a, str):\n",
    "            q_lst.append(q)\n",
    "            a_lst.append(a)\n",
    "data_af_length = len(q_lst)\n",
    "print('this sft dataset size is:', data_af_length - data_bf_length)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# HuatuoGPT-sft Dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-12-24T18:47:05.556072Z",
     "start_time": "2023-12-24T18:47:02.961722Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "226042it [00:02, 87515.66it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "this sft dataset size is: 226042\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "data_bf_length = len(q_lst)\n",
    "sft_data_path = './sft_data/medical/HuatuoGPT-sft-data/HuatuoGPT_sft_data_v1.jsonl'\n",
    "with open(sft_data_path,'r',encoding='utf-8') as f:\n",
    "    for row in tqdm(f):\n",
    "        line=json.loads(row)\n",
    "        q = line['data'][0].replace('\\n', '')\n",
    "        a = line['data'][1].replace('\\n', '')\n",
    "        if isinstance(q, str) and isinstance(a, str):\n",
    "            q_lst.append(q)\n",
    "            a_lst.append(a)\n",
    "data_af_length = len(q_lst)\n",
    "print('this sft dataset size is:', data_af_length - data_bf_length)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Chinese-medical-dialogue-data Dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-12-24T12:30:24.068489Z",
     "start_time": "2023-12-24T12:30:24.043088Z"
    }
   },
   "outputs": [],
   "source": [
    "# sft_data_path = './sft_data/medical/Chinese-medical-dialogue-data/IM.csv'\n",
    "# med_df_one = pd.read_csv(sft_data_path,encoding='utf-8')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# DISC-Med-SFT Dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-12-24T18:47:57.370483Z",
     "start_time": "2023-12-24T18:47:49.538818Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████████████████████████████████████████████████| 464898/464898 [00:05<00:00, 90313.12it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "this sft dataset size is: 1299289\n"
     ]
    }
   ],
   "source": [
    "data_bf_length = len(q_lst)\n",
    "sft_data_path = './sft_data/medical/DISC-Med-SFT/DISC-Med-SFT_released.jsonl'\n",
    "with open(sft_data_path, 'r') as f:  \n",
    "    for line in tqdm(f.readlines()):\n",
    "        line_dict = json.loads(line)\n",
    "        if len(line_dict['conversation'][::2]) != len(line_dict['conversation'][1::2]):\n",
    "            continue\n",
    "        for q in line_dict['conversation'][::2]:\n",
    "            q = q['content']\n",
    "            if isinstance(q, str):\n",
    "                q_lst.append(q)\n",
    "        \n",
    "        for a in line_dict['conversation'][1::2]:\n",
    "            a = a['content']\n",
    "            if isinstance(a, str):\n",
    "                a_lst.append(a)\n",
    "data_af_length = len(q_lst)\n",
    "print('this sft dataset size is:', data_af_length - data_bf_length)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# michaelwzhu Dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-12-24T18:48:07.765089Z",
     "start_time": "2023-12-24T18:48:03.892498Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "549326it [00:03, 142312.67it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "this sft dataset size is: 549326\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "data_bf_length = len(q_lst)\n",
    "sft_data_path = './sft_data/medical/michaelwzhu/ChatMed_Consult-v0.3.json'\n",
    "with open(sft_data_path,'r',encoding='utf-8') as f:\n",
    "    for row in tqdm(f):\n",
    "        line = json.loads(row)\n",
    "        q = line['query']\n",
    "        a = line['response']\n",
    "        if isinstance(q, str) and isinstance(a, str):\n",
    "            q_lst.append(q)\n",
    "            a_lst.append(a)\n",
    "data_af_length = len(q_lst)\n",
    "print('this sft dataset size is:', data_af_length - data_bf_length)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-12-24T18:48:09.614405Z",
     "start_time": "2023-12-24T18:48:08.628699Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "112565it [00:00, 115487.10it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "this sft dataset size is: 112565\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "data_bf_length = len(q_lst)\n",
    "sft_data_path = './sft_data/medical/michaelwzhu/ChatMed_TCM-v0.2.json'\n",
    "with open(sft_data_path,'r',encoding='utf-8') as f:\n",
    "    for row in tqdm(f):\n",
    "        line = json.loads(row)\n",
    "        q = line['query']\n",
    "        a = line['response']\n",
    "        if isinstance(q, str) and isinstance(a, str):\n",
    "            q_lst.append(q)\n",
    "            a_lst.append(a)\n",
    "data_af_length = len(q_lst)\n",
    "print('this sft dataset size is:', data_af_length - data_bf_length)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# write to csv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-12-24T18:48:11.964465Z",
     "start_time": "2023-12-24T18:48:11.949920Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(4254811, 4254811)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(q_lst), len(a_lst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-12-24T18:49:18.168251Z",
     "start_time": "2023-12-24T18:48:27.774040Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Totally sft dataset size is: 4254811\n"
     ]
    }
   ],
   "source": [
    "print('Totally sft dataset size is:', len(q_lst))\n",
    "df['prompt'] = q_lst\n",
    "df['answer'] = a_lst\n",
    "df.to_csv('./sft_data/sft_data.csv', index=False)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "dp",
   "language": "python",
   "name": "dp"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
